namespace TXLooker.WorkItemExtention.WIQL
{
    using System;
    using System.Reflection;

    public class NodeNotOperator : Node
    {
        private Node m_child;

        public NodeNotOperator() : base(NodeType.Not)
        {
        }

        public NodeNotOperator(Node node) : base(NodeType.Not)
        {
            this.m_child = node;
        }

        public override void Bind(IExternal e, NodeTableName tableContext, NodeFieldName fieldContext)
        {
            this.Value.Bind(e, tableContext, fieldContext);
            Tools.EnsureSyntax(this.Value.DataType == DataType.Bool, SyntaxError.ExpectingBoolean, this.Value);
            base.Bind(e, tableContext, fieldContext);
        }

        public override Node Optimize(IExternal e, NodeTableName tableContext, NodeFieldName fieldContext)
        {
            this.Value = this.Value.Optimize(e, tableContext, fieldContext);
            if (this.Value.NodeType == NodeType.BoolConst)
            {
                return new NodeBoolConst(!((NodeBoolConst) this.Value).Value);
            }
            if (this.Value.NodeType == NodeType.Not)
            {
                return ((NodeNotOperator) this.Value).Value;
            }
            return base.Optimize(e, tableContext, fieldContext);
        }

        public override string ToString()
        {
            return ("not " + base.JoinChildren(""));
        }

        public override int Count
        {
            get
            {
                return 1;
            }
        }

        public override DataType DataType
        {
            get
            {
                return DataType.Bool;
            }
        }

        public override bool IsConst
        {
            get
            {
                return this.m_child.IsConst;
            }
        }

        public override bool IsScalar
        {
            get
            {
                return false;
            }
        }

        public override Node this[int i]
        {
            get
            {
                return this.m_child;
            }
            set
            {
                this.m_child = value;
            }
        }

        public override Priority Priority
        {
            get
            {
                return Priority.UnaryBoolOperator;
            }
        }

        public Node Value
        {
            get
            {
                return this.m_child;
            }
            set
            {
                this.m_child = value;
            }
        }
    }
}

